package com.yanqu.road.server.gameplayer.module.vehicle.log;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.data.analytics.constant.EventDataConstants;
import com.yanqu.data.analytics.helper.DataBuilder;
import com.yanqu.road.entity.log.LogVehicleBoneEquip;
import com.yanqu.road.entity.player.UserVehicle;
import com.yanqu.road.entity.player.UserVehicleBone;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleBoneModule;
import com.yanqu.road.server.gameplayer.module.vehicle.VehicleModule;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.VehicleBoneConfigMgr;
import com.yanqu.road.server.manger.maidian.DefaultEventTraceAction;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class VehicleBoneLog {
    /**
     * 每日首次登录传数数
     *
     * @param player
     */
    public static void traceLoginBoneLog(GamePlayer player) {
        try {
            if (!DataAnalyticsMgr.openDataAnalytics()) return;
            VehicleModule vehicleModule = player.getModule(VehicleModule.class);

            //公共事件属性
            DataBuilder common = DataBuilder.newBuilder(player.getUserId(), DataAnalyticsMgr.getDistinctId(player.getUserInfo()));
            DataAnalyticsMgr.buildPublicEventData(player, common);

            for (Map.Entry<String, List<UserVehicleBone>> entry : player.getModule(VehicleBoneModule.class).getEquipBoneMap().entrySet()) {
                String vehicleUid = entry.getKey();
                UserVehicle userVehicle = vehicleModule.getUserVehicle(vehicleUid);
                if (userVehicle == null) {
                    continue;
                }

                List<UserVehicleBone> boneList = entry.getValue();
                List<JSONObject> boneJsonList = new ArrayList<>();
                int starAmount = 0;
                int holeNum = VehicleBoneConfigMgr.getVehicleBoneLvUnlockHoleNum(userVehicle.getBoneLv());
                for (UserVehicleBone userVehicleBone : boneList) {
                    boneJsonList.add(LogVehicleBoneEquip.parseEquipObject(userVehicleBone));
                    starAmount += VehicleBoneConfigMgr.getBoneSuitStar(GoodsMgr.getGoodsById(userVehicleBone.getConfigId()));
                }
                int combineAddQuantity = VehicleBoneConfigMgr.calcHoleFullAddQualification(userVehicle.getBoneLv(), holeNum, starAmount, userVehicle.getBoneHole());

                /*
                 * pet_name	珍兽ID
                 * pet_id	珍兽PID
                 * soulpow_level	当前魂力等级
                 * soulpow_pos	解锁槽位数
                 * soulpow_star	装备总星级
                 * soulpow_equip	装备魂骨
                 * soulpow_add	共鸣加资质
                 */
                DataBuilder dataBuilder = DataBuilder.newBuilder(player.getUserInfo().getUserId(), DataAnalyticsMgr.getDistinctId(player.getUserInfo()));
                dataBuilder.build("pet_name", userVehicle.getTemplateId());
                dataBuilder.build("pet_id", userVehicle.getVehicleUid());
                dataBuilder.build("soulpow_level", userVehicle.getBoneLv());
                dataBuilder.build("soulpow_star", starAmount);
                dataBuilder.build("soulpow_pos", holeNum);
                dataBuilder.build("soulpow_equip", boneJsonList);
                dataBuilder.build("soulpow_add", combineAddQuantity);

                DataAnalyticsMgr.provider.track(dataBuilder.getAccountId(), dataBuilder.getDistinctId(), "soulpow_info", dataBuilder.getDataMap());
            }

        } catch (Exception e) {
            DataAnalyticsMgr.getLogger().error(" error ", e);
        }
    }
}
